//
//  CustomRefreshHeader.m
//  SheMeasured
//
//  Created by ddkj on 2019/5/8.
//  Copyright © 2019 xmddkj. All rights reserved.
//

#import "CustomRefreshHeader.h"

@interface CustomRefreshHeader () {
        NSInteger _panState; //0: default, 1: begin, 2: change, 3: end
        BOOL _enableOpt;
}

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIImageView *imgView;

@end

@implementation CustomRefreshHeader

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare {
    [super prepare];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = RGBSAME(245);
    [self addSubview:bgView];
    self.bgView = bgView;
    
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    imgView.image = [UIImage imageNamed:@"tab_home_sel"];
    imgView.animationImages = @[
                                [UIImage imageNamed:@"tab_home_sel"],
                                [UIImage imageNamed:@"tab_data_sel"],
                                [UIImage imageNamed:@"tab_mine_sel"]
                                ];
    imgView.animationDuration = 0.5;
    imgView.animationRepeatCount = 0;
    [self addSubview:imgView];
    self.imgView = imgView;
    
    // label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = RGBSAME(51);
    label.font = FONTSIZE(12);
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    _enableOpt = YES;
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews {
    [super placeSubviews];
    
    self.bgView.frame = self.bounds;
    
    CGFloat width = 20;
    
    [self.label sizeToFit];
    self.label.frame = CGRectMake(self.centerX - self.label.width/2 + 13, 0, self.label.width, self.height) ;
    
    self.imgView.frame = CGRectMake(self.label.left - width - 6, (self.height-width)/2, width, width);
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change {
    [super scrollViewContentOffsetDidChange:change];
    
    //NSLog(@"contentOffset: %@", change);
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change {
    [super scrollViewContentSizeDidChange:change];
    
    //NSLog(@"contentSize: %@", change);
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change {
    [super scrollViewPanStateDidChange:change];
    
    _panState = [change[@"new"] integerValue];
    
    // begin
    if (_panState == 1) {
        
    }
    // change
    else if (_panState == 2) {
        
    }
    // end
    else if (_panState == 3) {
        
    }
}
#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state {
    MJRefreshCheckState;
    
    NSLog(@"refresh: %@", @[@"", @"普通闲置状态", @"下拉中", @"刷新中", @"即将刷新", @"无数据"][state]);
    
    switch (state) {
            
        case MJRefreshStateIdle: {
            self.label.text = @"下拉刷新";
        }
            break;
            
        case MJRefreshStatePulling: {
            self.label.text = @"松开刷新";
        }
            break;
            
        case MJRefreshStateRefreshing: {
            self.label.text = @"加载中...";
            [self showCar:YES];
        }
            break;
            
        case MJRefreshStateWillRefresh: {
            
        }
            break;
            
        default:
            break;
    }
}





#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent {
    [super setPullingPercent:pullingPercent];
    
    //    // 1.0 0.5 0.0
    //    // 0.5 0.0 0.5
    //    CGFloat red   = 1.0 - pullingPercent * 0.5;
    //    CGFloat green = 0.5 - 0.5 * pullingPercent;
    //    CGFloat blue  = 0.5 * pullingPercent;
    //    self.label.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}




#pragma mark - custom code

- (void)showCar:(BOOL)show {
    if (show) {
        [self.imgView startAnimating];
    } else {
        [self.imgView stopAnimating];
    }
}
- (void)endRefreshing {
    if (_enableOpt) {
        _enableOpt = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self showCar:NO];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [super endRefreshing];
                self->_enableOpt = YES;
            });
        });
    }
}

@end
